﻿@model IEnumerable<PrePurchaseRequestItem>

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "แจ้งความประสงค์ขอซื้อ/จ้าง";
    ViewBag.Descr = "ข้อมูลที่หน่วยงานต่างๆ ส่งความประสงค์ขอซื้อ/จ้างเข้ามาที่หน่วยงานพัสดุ";

    int intPprId = ViewBag.PprId;
    PrePurchaseRequest ppr = (PrePurchaseRequest)ViewBag.PrePurchaseRequest;
    var projectService = new KPIS.GERP.WEB.GIMS.ProjectOperationService.ProjectOperationServiceClient();
    
    decimal? sumAmount = 0;
}

@using (Html.BeginForm("DeleteAll", "PrePurchaseRequestItem", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    
    <div class="row-fluid">    
        @if(ViewBag.EmptyLayout != true || (ppr.RecordStatus == RecordStatus.ReturnedStatus))
        {
            <div class="block-button-menu-above">
                @if (ViewBag.bEditable)
                {
                    @Html.ActionLink("เพิ่ม", "Create", new {PprId = intPprId}
                                     , new {@class = "btn btn-small btn-success button-standard-width", role = "button"})
                }
                @Html.ButtonDeleteAll()
            </div>
        }

        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
					@ViewBag.Title
                </div>
				@Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width:50px;vertical-align: text-top " >
						<!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox" />
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th style="width: 100px; vertical-align: text-top" valign="top">
                        รหัสกิจกรรม
                    </th>
                    <th style="width: 80px; vertical-align: text-top" valign="top">
                        แหล่งเงิน
                    </th>
                    <th style="width: 80px; vertical-align: text-top">
                        รายการพัสดุ/บริการ
                    </th>
                    <th style=" vertical-align: text-top">
                        @Html.DisplayNameFor(model => model.Descr)
                    </th>
                    <th style="width: 50px; vertical-align: text-top">
                        @Html.DisplayNameFor(model => model.Qty)
                    </th>
                    <th style="width: 50px; vertical-align: text-top">
                        @Html.DisplayNameFor(model => model.Price)
                    </th>
                    <th style="width: 50px; vertical-align: text-top">
                        @Html.DisplayNameFor(model => model.Amount)
                    </th>
                    @*<th>
                        @Html.DisplayNameFor(model => model.RecordStatus)
                    </th>
                    <th>
						@KPIS.GERP.Models.Utility.LookUpTypeNameCaption(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.Priority)
					</th>*@
					<th class="center" style="width:80px;">
						<!-- Empty head column for export buttons -->&nbsp;
					</th>
                </tr>
            </thead>

            <tbody>
                @foreach (var m in Model ?? new List<PrePurchaseRequestItem>())
                {
                    <tr>
                        <td class="center">
							<!-- Check box all for delete multiple records -->
                            <label>
                                <input type="checkbox" name="deleteIds" value="@(m.PprItemId)" />
                                <span class="lbl"></span>
                            </label>
                        </td>
                        <td>
                            @*{
                    int ProjId =
                        m.ProjectOperationExpense.ProjectExpense.Project.ProjId;
                    Decimal TotalAmount =
                        projectService.SelectProjectExpense(new ProjectExpense { ProjId = ProjId }, null)
                            .ToList()
                            .Sum(pe => pe.TotalAmount) ?? 0;
                    string StrBgmName =
                        m.ProjectOperationExpense.ProjectExpense.Project.Name;
                    string[] StrC = StrBgmName.Split(')');
                    string BgmName = StrC[0];
                    string BgmNo = StrC.Count() > 1 ? StrC[1] : "";

                    if (!string.IsNullOrWhiteSpace(BgmNo))
                    {
                        @:@BgmNo
                                }
                    else if (StrBgmName.Split(new[] { "รหัสกิจกรรม" }, StringSplitOptions.None).Count() >= 2)
                    {
                        @:@StrBgmName.Split(new[] { "รหัสกิจกรรม" }, StringSplitOptions.None)[1]
                                }
                            }*@
                            
                            @(m.ProjectOperationExpense != null &&
                            m.ProjectOperationExpense.ProjectOperation != null
                            ? m.ProjectOperationExpense.ProjectOperation.Code : "-")

                        </td>

                        <td>
                            @Html.DisplayFor(model => m.ProjectOperationExpense.ProjectExpense.MasBudgetSource.Name)
                        </td>
                        <td>
                            @if (m.HaveItem ?? true)
                            {
                                if(ViewBag.EmptyLayout == true)
                                {
                                    @m.MasResourceItem.Name
                                } else
                                {
                                    @Html.ActionLink(@m.MasResourceItem.Name, "Details", new { Id = m.PprItemId })
                                }
                            }
                            else
                            {
                                <span class="red">ไม่ระบุ</span>
                            }
                        </td>
                        <td>
							@Html.Raw((m.Descr ?? "").Replace("\n", "<br/>"))
						</td>

						<td style="text-align:right">
							@(m.Qty.HasValue ? m.Qty.Value.ToString("#,##0") : "0")
						</td>
						<td style="text-align:right">
							@(m.Price.HasValue ? m.Price.Value.ToString("#,##0.00") : "0")
						</td>
						<td style="text-align:right">
							@(m.Amount.HasValue ? m.Amount.Value.ToString("#,##0.00") : "0")
                            @if (m.Amount.HasValue) {
                                sumAmount = sumAmount + m.Amount;
                            }
						</td>
                        <td class="td-actions">
                             @if(ViewBag.EmptyLayout != true)
                             {
                                 if (ViewBag.bEditable)
                                 {
                                     @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                         {
                                             Id = m.PprId,
                                             DetailsUrl = Url.Action("Details") + "/" + m.PprItemId,
                                             EditUrl = Url.Action("Edit") + "/" + m.PprItemId,
                                             DeleteUrl = Url.Action("Delete") + "?Id=" + m.PprItemId + "&PprId=" + m.PprId
                                         })
                                 }
                                 else
                                 {
                                     @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                         {
                                             Id = m.PprId,
                                             DetailsUrl = Url.Action("Details") + "/" + m.PprItemId
                                         })
                                 }
                             }
                        </td>
                    </tr>
                }

               

            </tbody>
        </table>
        <br />
        <div style="font-size:large;color:blue;text-align:right;padding-right:7em;">
            @if (sumAmount.HasValue){
                @Html.Raw("ยอดรวมทั้งหมด = ")
                    @Html.Raw(sumAmount.Value.ToString("#,##0.00"))
            }
            </div>
    </div>
}

@section scripts {
    
    <script type="text/javascript">
        $(function() {
            var oTable1 = $('#table').dataTable({
                "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
                ]
            });

            $('table th input:checkbox').on('click', function() {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                    .each(function() {
                        this.checked = that.checked;
                        $(this).closest('tr').toggleClass('selected');
                    });

            });

            $('[data-rel=tooltip]').tooltip();

            $("form").submit(function() {
                var currentForm = this;
                bootbox.confirm(function(result) {
                    var _result = result;
                    if (result) {
                        $.ajax({
                            url: '@(Url.Action("DeleteAll", "PrePurchaseItem"))',
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            success: function(result) {
                                if (result.success) {
                                    location.reload();
                                } else {
                                }
                            }
                        });
                    }
                });
                return false;
            });


            try {
                //.removeClass( "disabled" ).addClass( "disabled" );
                @if (Model != null && Model.Any())
                {
                    <text> 
                        $('#btnAskApproval', window.parent.document).removeClass("disabled");
                        $('#btnAskApproval', window.parent.document).attr("title", "");
                    </text>
                }
                else
                {
                    <text> 
                        $('#btnAskApproval', window.parent.document).addClass("disabled");
                        $('#btnAskApproval', window.parent.document).attr("title", "กรุณาเลือกพัสดุ/บริการที่ซื้อ/จ้างก่อน");
                    </text>
                }
            } catch(Exception) {
                alert(Exception);
            }
        });        
         
    </script>

}